<?php

class Menu {

    private static $_controller;

    public static function getMenu($controller) {

        self::$_controller = $controller;
        $items = array(
            array('label' => '<span class="icon-list-alt"></span> Dashboard', 'url' => array('/site/index')),
            array('label' => '<span class="icon-book"></span>  CatÃ¡logos', 'url' => '#', 'items' => array(
                    array('label' => 'Industrias', 'url' => array('/crm/industria/admin'), 'access' => 'action_industria_admin', 'active_rules' => array('module' => 'crm', 'controller' => 'industria')),
                )),
        );
        return self::generateMenu($items);
    }

    public static function getAdminMenu($controller) {
        self::$_controller = $controller;
        $items = array(
            array('label' => '<span class="icon-list-alt"></span>  Dashboard', 'url' => array('/site/index')),
            array('label' => '<span class="icon-tasks"></span> Administración', 'url' => Yii::app()->user->ui->userManagementAdminUrl),
            array('label' => '<span class="icon-globe"></span>  Ubicaciones', 'url' => '#', 'items' => array(
                    array('label' => '<span class=""></span> Paises', 'url' => array('/direcciones/paises/admin'), 'access' => 'action_paises_admin', 'active_rules' => array('module' => 'direcciones', 'controller' => 'paises')),
                    array('label' => '<span class=""></span> Provincias', 'url' => array('/direcciones/provincias/admin'), 'access' => 'action_provincias_admin', 'active_rules' => array('module' => 'direcciones', 'controller' => 'provincias')),
                    array('label' => '<span class=""></span> Ciudades', 'url' => array('/direcciones/ciudad/admin'), 'access' => 'action_ciudad_admin', 'active_rules' => array('module' => 'direcciones', 'controller' => 'ciudad')),
                    array('label' => '<span class="icon-road"></span> Direcciones', 'url' => array('/direcciones/direcciones/admin'), 'access' => 'action_direcciones_admin', 'active_rules' => array('module' => 'direcciones', 'controller' => 'direcciones')),
                )),
            array('label' => '<span class="icon-group"></span>  Cuentas', 'url' => '#', 'items' => array(
                    array('label' => '<span class="icon-user"></span> Contactos', 'url' => array('/cuentas/contactos/admin'), 'access' => 'action_contactos_admin', 'active_rules' => array('module' => 'cuentas', 'controller' => 'contactos')),
                    array('label' => '<span class="icon-briefcase"></span> Empresas', 'url' => array('/cuentas/empresas/admin'), 'access' => 'action_empresas_admin', 'active_rules' => array('module' => 'cuentas', 'controller' => 'empresas')),
                )),
            array('label' => '<span class=" icon-truck"></span>  Inventario', 'url' => '#', 'items' => array(
                    array('label' => '<span class=" icon-shopping-cart"></span> Compras', 'url' => array('/inventario/kardexCompra/admin'), 'access' => 'action_kardexCompra_admin', 'active_rules' => array('module' => 'inventario', 'controller' => 'kardexCompra')),
                    array('label' => '<span class=" icon-money"></span> Ventas', 'url' => array('/inventario/kardexVenta/admin'), 'access' => 'action_kardexVenta_admin', 'active_rules' => array('module' => 'inventario', 'controller' => 'kardexVenta')),
                )),
            array('label' => '<span class=" icon-"></span>  Publicidad', 'url' => '#', 'items' => array(
                    array('label' => '<span class=" "></span> Pagos publicaciones', 'url' => array('/publicidad/pagoPublicacion/admin'), 'access' => 'action_pagoPublicacion_admin', 'active_rules' => array('module' => 'publicidad', 'controller' => 'pagoPublicacion')),
                    array('label' => '<span class=" "></span> Preguntas', 'url' => array('/publicidad/preguntas/admin'), 'access' => 'action_preguntas_admin', 'active_rules' => array('module' => 'publicidad', 'controller' => 'preguntas')),
                    array('label' => '<span class=""></span> Respuestas', 'url' => array('/publicidad/respuestas/admin'), 'access' => 'action_respuestas_admin', 'active_rules' => array('module' => 'publicidad', 'controller' => 'respuestas')),
                )),
            array('label' => '<span class="icon-leaf"></span>  Almacen', 'url' => '#', 'items' => array(
                    array('label' => '<span class="icon-book"></span> Articulos', 'url' => array('/producto/tipoArticulo/admin'), 'access' => 'action_tipoArticulo_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'tipoArticulo')),
                    array('label' => '<span class="icon-tag"></span> Categorías', 'url' => array('/producto/categoria/admin'), 'access' => 'action_categoria_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'categoria')),
                    array('label' => '<span class="icon-tasks"></span> Subcategorías', 'url' => array('/producto/subcategoria/admin'), 'access' => 'action_subcategoria_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'subcategoria')),
                    array('label' => '<span class="icon-barcode"></span> Productos', 'url' => array('/producto/productos/admin'), 'access' => 'action_productos_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'productos')),
                    array('label' => '<span class="icon-list-alt "></span> Cartas de Presentación', 'url' => array('/producto/cartaPresentacion/admin'), 'access' => 'action_cartaPresentacion_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'cartaPresentacion')),
                    array('label' => '<span class="icon-picture"></span> Galería', 'url' => array('/producto/imagenes/admin'), 'access' => 'action_imagenes_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'imagenes')),
                    array('label' => '<span class="icon-gift"></span> Promociones', 'url' => array('/producto/promociones/admin'), 'access' => 'action_promociones_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'promociones')),
                    array('label' => '<span class="icon-strikethrough"></span> Descuentos', 'url' => array('/producto/descuentos/admin'), 'access' => 'action_descuentos_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'descuentos')),
                    array('label' => '<span class="icon-refresh"></span> Intercambios', 'url' => array('/producto/intercambios/admin'), 'access' => 'action_intercambios_admin', 'active_rules' => array('module' => 'producto', 'controller' => 'intercambios')),
                )),
        );

        return self::generateMenu($items);
    }

    /**
     * Function to create a menu with acces rules and active item
     * @param array $items items to build the menu
     * @return array the formated menu
     */
    private static function generateMenu($items) {
        $menu = array();

        foreach ($items as $k => $item) {
            $access = false;
            $menu_item = $item;

            // Check children access
            if (isset($item['items'])) {
                $menu_item['items'] = array();
                // Check childrens access
                foreach ($item['items'] as $j => $children) {
                    if ($access = Yii::app()->user->checkAccess($children['access'])) {
                        $menu_item['items'][$j] = $children;
                        if (isset($children['active_rules']) && self::getActive($children['active_rules'])) {
                            $menu_item['items'][$j]['active'] = true;
                            $menu_item['active'] = true;
                        }
                    }
                }
            } else {
                // Check item access
                if (isset($item['access'])) {
                    $access = Yii::app()->user->checkAccess($item['access']);
                } else {
                    $access = true;
                }
                // Check active
                if (isset($item['active_rules'])) {
                    $menu_item['active'] = self::getActive($item['active_rules']);
                }
            }

            // If acces to the item or any child add to the menu
            if ($access) {
                $menu[] = $menu_item;
            }
        }

        return $menu;
    }

    /**
     * Function to compare the menu active rules with the current url
     * @param array $active_rules the array of rules to compare
     * @return boolean true if the rules match the current url
     */
    private static function getActive($active_rules) {
        $current = false;
        if (self::$_controller) {
            if (isset($active_rules['module']) && self::$_controller->module) {
                $current = (self::$_controller->module->id == $active_rules['module']);
            }
            if (isset($active_rules['controller'])) {
                $current = (self::$_controller->id == $active_rules['controller']);
            }
            if (isset($active_rules['action'])) {
                $current = (self::$_controller->action->id == $active_rules['action']);
            }
        }
        return $current;
    }

}
